package kotan.view.table;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;


public class EntityTableContextMenu {

    public void show(MouseEvent event, final EntityTableView view) {
        // TODO refactoring
        final int row = view.table.convertRowIndexToModel(view.table.getSelectedRow());
        final int col = view.table.convertColumnIndexToModel(view.table.getSelectedColumn());
        if (row == -1 || col == -1) return;
        Object colValue = view.tableModel.getValueAt(row, col);
        JPopupMenu menu = new JPopupMenu();
        // delete
        JMenuItem deleteMenu = new JMenuItem("delete entity");
        deleteMenu.setEnabled(view.tableModel.isKey(col));
        menu.add(deleteMenu);
        // set null
        JMenuItem nullMenu = new JMenuItem("set null value");
        nullMenu.setEnabled(colValue != null && !view.tableModel.isKey(col));
        nullMenu.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                view.control.setNullAt(row, col);
            }
        });
        menu.add(nullMenu);
        menu.show(event.getComponent(), event.getPoint().x, event.getPoint().y);
    }

}
